package com.qijia.travel.app;

import java.math.BigDecimal;

import javax.servlet.http.HttpSession;

import com.qijia.travel.common.Constants;
import com.qijia.travel.common.SqlMappedId;
import com.qijia.travel.dao.BaseDAO;
import com.qijia.travel.dao.SingletonDaoFactory;
import com.qijia.travel.dao.TravelDbException;
import com.qijia.travel.domain.Guide;
import com.qijia.travel.domain.PageResult;
import com.qijia.travel.domain.TouristExpenditure;
import com.qijia.travel.domain.example.ExampleFactory;
import com.qijia.travel.domain.example.TouristExpenditureExample;
import com.qijia.travel.helper.DateHelper;
import com.qijia.travel.vo.TouristDetailVo;

public class EptManager {
	private BaseDAO baseDao;

	public EptManager() {
		try {
			baseDao = SingletonDaoFactory.createDAO();
		} catch (TravelDbException e) {
			e.printStackTrace();
		}
	}

	public BigDecimal getOutTotal(TouristDetailVo vo) throws Exception {
		TouristExpenditureExample example = ExampleFactory
				.createTouristExpenditureExample();
		example.createCriteria(vo);

		return (BigDecimal) baseDao.selectByExample(
				SqlMappedId.TOURIST_EXPENDITURE + "."
						+ SqlMappedId.sumExpenditureByExample, example).get(0);
	}

	public TouristExpenditure getTouristExpenditureById(int statusId)
			throws AppErrorException {
		TouristExpenditure status = new TouristExpenditure();
		status.setExpenditureId(statusId);

		try {
			return (TouristExpenditure) baseDao.selectByPrimaryKey(
					SqlMappedId.TOURIST_EXPENDITURE + "."
							+ SqlMappedId.selectByPrimaryKey, status);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取支出信息失败!");
		}
	}

	public void createTouristExpenditure(TouristExpenditure status,
			HttpSession session) throws AppErrorException {
		if (status == null)
			throw new AppErrorException("请输入新增的支出信息");
		try {
			if (status.getExpenditureDt() != null)
				status.setExpenditureDate(DateHelper.parseToDate(status
						.getExpenditureDt(), DateHelper.yyyyMMdd));
			if (status.getExpenditureName() != null)
				baseDao.insert(SqlMappedId.TOURIST_EXPENDITURE + "."
						+ SqlMappedId.insert, status);
		} catch (TravelDbException e) {
			throw new AppErrorException("注册新支出信息失败,请稍候重试!", e);
		}

		Guide guide = (Guide) session.getAttribute(Constants.USER_KEY);
		AccLogManager.saveInsertAccLog(guide.getGuideId(), status);
	}

	public void updateTouristExpenditure(TouristExpenditure status,
			HttpSession session) throws AppErrorException {
		if (status == null)
			throw new AppErrorException("请选择要进行修改的支出信息");
		try {
			if (status.getExpenditureDt() != null)
				status.setExpenditureDate(DateHelper.parseToDate(status
						.getExpenditureDt(), DateHelper.yyyyMMdd));
			baseDao.updateByPrimaryKeySelective(SqlMappedId.TOURIST_EXPENDITURE
					+ "." + SqlMappedId.updateByPrimaryKeySelective, status);
		} catch (TravelDbException e) {
			throw new AppErrorException("更新选中的支出信息["
					+ status.getExpenditureName() + "]失败,请稍候重试!", e);
		}

		Guide guide = (Guide) session.getAttribute(Constants.USER_KEY);
		AccLogManager.saveUpdateAccLog(guide.getGuideId(), status);
	}

	public void deleteTouristExpenditure(int statusId, HttpSession session)
			throws AppErrorException {
		if (statusId == 0)
			throw new AppErrorException("请选择要进行删除的支出信息");
		TouristExpenditure status = new TouristExpenditure();
		try {
			status = this.getTouristExpenditureById(statusId);
			if (status != null)
				baseDao.deleteByPrimaryKey(SqlMappedId.TOURIST_EXPENDITURE
						+ "." + SqlMappedId.deleteByPrimaryKey, status);
		} catch (TravelDbException e) {
			throw new AppErrorException("删除选中的支出信息[编号为:"
					+ status.getExpenditureId() + "]失败,请稍候重试!", e);
		}

		Guide guide = (Guide) session.getAttribute(Constants.USER_KEY);
		AccLogManager.saveDeleteAccLog(guide.getGuideId(), status);
	}

	public PageResult selectTouristExpenditureList(TouristDetailVo vo)
			throws AppErrorException {
		TouristExpenditureExample example = ExampleFactory
				.createTouristExpenditureExample();
		example.createCriteria(vo);
		example.setOrderByClause(" io_type desc, expenditure_create_date desc ");

		try {
			PageResult page = PageResult.newInstance();
			page.setSelectDataMappedId(SqlMappedId.TOURIST_EXPENDITURE + "."
					+ SqlMappedId.selectByExample);
			page.setCountMappedId(SqlMappedId.TOURIST_EXPENDITURE + "."
					+ SqlMappedId.countByExample);
			page.setSqlParamObj(example);

			if (vo.getCurPage() == null)
				vo.setCurPage(1);
			else if (vo.getCurPage() != null && vo.getCurPage() <= 1)
				vo.setCurPage(1);

			page.setCurPage(vo.getCurPage());
			page.setPageSize(20);

			return baseDao.selectByPage(page);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取支出信息数据列表失败,请稍候重试!", e);
		} catch (InstantiationException e) {
			throw new AppErrorException("获取支出信息数据列表失败,请稍候重试!", e);
		} catch (IllegalAccessException e) {
			throw new AppErrorException("获取支出信息数据列表失败,请稍候重试!", e);
		}
	}
}
